home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch6 / BinCont.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-25  |  9.1 KB  |  276 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmBinCont 
  4.    Caption         =   "BinCont []"
  5.    ClientHeight    =   4800
  6.    ClientLeft      =   165
  7.    ClientTop       =   735
  8.    ClientWidth     =   5160
  9.    LinkTopic       =   "Form2"
  10.    ScaleHeight     =   4800
  11.    ScaleWidth      =   5160
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin MSComDlg.CommonDialog dlgOpenFile 
  14.       Left            =   0
  15.       Top             =   840
  16.       _ExtentX        =   847
  17.       _ExtentY        =   847
  18.       _Version        =   393216
  19.    End
  20.    Begin VB.PictureBox picHistogram 
  21.       Height          =   1815
  22.       Left            =   120
  23.       ScaleHeight     =   117
  24.       ScaleMode       =   3  'Pixel
  25.       ScaleWidth      =   325
  26.       TabIndex        =   2
  27.       Top             =   0
  28.       Width           =   4935
  29.    End
  30.    Begin VB.PictureBox picOriginal 
  31.       AutoSize        =   -1  'True
  32.       Height          =   2775
  33.       Left            =   120
  34.       ScaleHeight     =   181
  35.       ScaleMode       =   3  'Pixel
  36.       ScaleWidth      =   157
  37.       TabIndex        =   1
  38.       Top             =   1920
  39.       Width           =   2415
  40.    End
  41.    Begin VB.PictureBox picResult 
  42.       Height          =   2775
  43.       Left            =   2640
  44.       ScaleHeight     =   181
  45.       ScaleMode       =   3  'Pixel
  46.       ScaleWidth      =   157
  47.       TabIndex        =   0
  48.       Top             =   1920
  49.       Width           =   2415
  50.    End
  51.    Begin VB.Menu mnuFile 
  52.       Caption         =   "&File"
  53.       Begin VB.Menu mnuFileOpen 
  54.          Caption         =   "&Open..."
  55.          Shortcut        =   ^O
  56.       End
  57.       Begin VB.Menu mnuFileSaveAs 
  58.          Caption         =   "Save &As..."
  59.          Shortcut        =   ^A
  60.       End
  61.    End
  62. Attribute VB_Name = "frmBinCont"
  63. Attribute VB_GlobalNameSpace = False
  64. Attribute VB_Creatable = False
  65. Attribute VB_PredeclaredId = True
  66. Attribute VB_Exposed = False
  67. Option Explicit
  68. ' Arrange the controls.
  69. Private Sub ArrangeControls()
  70. Dim wid As Single
  71.     ' Position the result PictureBox.
  72.     picResult.Move _
  73.         picOriginal.Left + picOriginal.Width + 120, _
  74.         picOriginal.Top, _
  75.         picOriginal.Width, _
  76.         picOriginal.Height
  77.     picResult.Cls
  78.     ' This makes the image resize itself to
  79.     ' fit the picture.
  80.     picResult.Picture = picResult.Image
  81.     ' Make the form big enough.
  82.     wid = picResult.Left + picResult.Width
  83.     If wid < picHistogram.Left + picHistogram.Width Then _
  84.         wid = picHistogram.Left + picHistogram.Width
  85.     Width = wid + Width - ScaleWidth + 120
  86.     Height = picResult.Top + picResult.Height + _
  87.         Height - ScaleHeight + 120
  88.     DoEvents
  89. End Sub
  90. ' Transform the image.
  91. Private Sub TransformImage(ByVal cutoff As Single)
  92. Dim pixels() As RGBTriplet
  93. Dim bits_per_pixel As Integer
  94. Dim brightness As Integer
  95. Dim X As Integer
  96. Dim Y As Integer
  97.     ' Get the pixels from picOriginal.
  98.     GetBitmapPixels picOriginal, pixels, bits_per_pixel
  99.     ' Set the pixel color values.
  100.     For Y = 0 To picOriginal.ScaleHeight - 1
  101.         For X = 0 To picOriginal.ScaleWidth - 1
  102.             With pixels(X, Y)
  103.                 brightness = (CInt(.rgbRed) + _
  104.                     .rgbGreen + .rgbBlue) / 3
  105.                 If brightness >= cutoff Then
  106.                     .rgbRed = 255
  107.                     .rgbGreen = 255
  108.                     .rgbBlue = 255
  109.                 Else
  110.                     .rgbRed = 0
  111.                     .rgbGreen = 0
  112.                     .rgbBlue = 0
  113.                 End If
  114.             End With
  115.         Next X
  116.     Next Y
  117.     ' Set picResult's pixels.
  118.     SetBitmapPixels picResult, bits_per_pixel, pixels
  119.     picResult.Picture = picResult.Image
  120. End Sub
  121. ' Show the brightness histogram.
  122. Private Sub ShowHistogram(ByVal picImage As PictureBox)
  123. Dim counts(0 To 255) As Long
  124. Dim max_count As Long
  125. Dim brightness As Integer
  126. Dim pixels() As RGBTriplet
  127. Dim bits_per_pixel As Integer
  128. Dim X As Integer
  129. Dim Y As Integer
  130. Dim i As Integer
  131.     ' Clear the previous results.
  132.     picHistogram.Line _
  133.         (picHistogram.ScaleLeft, picHistogram.ScaleTop)- _
  134.         Step(picHistogram.ScaleWidth, picHistogram.ScaleHeight), _
  135.         picHistogram.BackColor, BF
  136.     picHistogram.Refresh
  137.     ' Get the pixels from picImage.
  138.     GetBitmapPixels picImage, pixels, bits_per_pixel
  139.     ' Count the brightness values.
  140.     For Y = 0 To picImage.ScaleHeight - 1
  141.         For X = 0 To picImage.ScaleWidth - 1
  142.             With pixels(X, Y)
  143.                 brightness = (CInt(.rgbRed) + _
  144.                     .rgbGreen + .rgbBlue) / 3
  145.                 counts(brightness) = counts(brightness) + 1
  146.             End With
  147.         Next X
  148.     Next Y
  149.     ' Find the largest count value.
  150.     ' Skip value 0. There tend to be a lot of
  151.     ' them and they dominate things.
  152.     For i = 1 To 255
  153.         If max_count < counts(i) _
  154.             Then max_count = counts(i)
  155.     Next i
  156.     ' Display the brightness histogram.
  157.     picHistogram.ScaleTop = 1.1 * max_count
  158.     picHistogram.ScaleHeight = -1.2 * max_count
  159.     picHistogram.ScaleLeft = -1
  160.     picHistogram.ScaleWidth = 258
  161.     For brightness = 0 To 255
  162.         If counts(brightness) > 0 Then _
  163.             picHistogram.Line (brightness, 0)-(brightness + 1, counts(brightness)), , BF
  164.     Next brightness
  165.     ' Make the changes permanent.
  166.     picHistogram.Picture = picHistogram.Image
  167. End Sub
  168. ' Start in the current directory.
  169. Private Sub Form_Load()
  170.     picOriginal.AutoSize = True
  171.     picOriginal.ScaleMode = vbPixels
  172.     picOriginal.AutoRedraw = True
  173.     picResult.ScaleMode = vbPixels
  174.     picResult.AutoRedraw = True
  175.     picHistogram.AutoRedraw = True
  176.     dlgOpenFile.CancelError = True
  177.     dlgOpenFile.InitDir = App.Path
  178.     dlgOpenFile.Filter = _
  179.         "Bitmaps (*.bmp)|*.bmp|" & _
  180.         "GIFs (*.gif)|*.gif|" & _
  181.         "JPEGs (*.jpg)|*.jpg;*.jpeg|" & _
  182.         "Icons (*.ico)|*.ico|" & _
  183.         "Cursors (*.cur)|*.cur|" & _
  184.         "Run-Length Encoded (*.rle)|*.rle|" & _
  185.         "Metafiles (*.wmf)|*.wmf|" & _
  186.         "Enhanced Metafiles (*.emf)|*.emf|" & _
  187.         "Graphic Files|*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf|" & _
  188.         "All Files (*.*)|*.*"
  189. End Sub
  190. ' Load the indicated file.
  191. Private Sub mnuFileOpen_Click()
  192. Dim file_name As String
  193.     ' Let the user select a file.
  194.     On Error Resume Next
  195.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  196.     dlgOpenFile.ShowOpen
  197.     If Err.Number = cdlCancel Then
  198.         Exit Sub
  199.     ElseIf Err.Number <> 0 Then
  200.         Beep
  201.         MsgBox "Error selecting file.", , vbExclamation
  202.         Exit Sub
  203.     End If
  204.     On Error GoTo 0
  205.     Screen.MousePointer = vbHourglass
  206.     DoEvents
  207.     file_name = Trim$(dlgOpenFile.FileName)
  208.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  209.         - Len(dlgOpenFile.FileTitle) - 1)
  210.     Caption = "BinCont [" & dlgOpenFile.FileTitle & "]"
  211.     ' Open the original file.
  212.     On Error GoTo LoadError
  213.     picOriginal.Picture = LoadPicture(file_name)
  214.     On Error GoTo 0
  215.     ' Make picResult the same size and position it.
  216.     ArrangeControls
  217.     ' Make picResult show the same image.
  218.     picResult.Picture = picOriginal.Picture
  219.     DoEvents
  220.     ' Display the brightness histogram.
  221.     ShowHistogram picOriginal
  222.     Screen.MousePointer = vbDefault
  223.     Exit Sub
  224. LoadError:
  225.     Screen.MousePointer = vbDefault
  226.     MsgBox "Error " & Format$(Err.Number) & _
  227.         " opening file '" & file_name & "'" & vbCrLf & _
  228.         Err.Description
  229. End Sub
  230. ' Save the transformed image.
  231. Private Sub mnuFileSaveAs_Click()
  232. Dim file_name As String
  233.     ' Let the user select a file.
  234.     On Error Resume Next
  235.     dlgOpenFile.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  236.     dlgOpenFile.ShowSave
  237.     If Err.Number = cdlCancel Then
  238.         Exit Sub
  239.     ElseIf Err.Number <> 0 Then
  240.         Beep
  241.         MsgBox "Error selecting file.", , vbExclamation
  242.         Exit Sub
  243.     End If
  244.     On Error GoTo 0
  245.     Screen.MousePointer = vbHourglass
  246.     DoEvents
  247.     file_name = Trim$(dlgOpenFile.FileName)
  248.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  249.         - Len(dlgOpenFile.FileTitle) - 1)
  250.     Caption = "BinCont [" & dlgOpenFile.FileTitle & "]"
  251.     ' Save the transformed image into the file.
  252.     On Error GoTo SaveError
  253.     SavePicture picResult.Picture, file_name
  254.     On Error GoTo 0
  255.     Screen.MousePointer = vbDefault
  256.     Exit Sub
  257. SaveError:
  258.     Screen.MousePointer = vbDefault
  259.     MsgBox "Error " & Format$(Err.Number) & _
  260.         " saving file '" & file_name & "'" & vbCrLf & _
  261.         Err.Description
  262. End Sub
  263. ' Set the binary contrast enhancement level.
  264. Private Sub picHistogram_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  265.     If picOriginal.Picture <> 0 Then
  266.         picHistogram.Cls
  267.         picHistogram.Line _
  268.             (X, picHistogram.ScaleTop)- _
  269.             Step(0, picHistogram.ScaleHeight), vbRed
  270.         Screen.MousePointer = vbHourglass
  271.         DoEvents
  272.         TransformImage X
  273.         Screen.MousePointer = vbDefault
  274.     End If
  275. End Sub
  276.